<style>
    #form .mint-popup.mint-popup-bottom {
        width: 100%;
    }

    .mint-actionsheet-list {
        max-height: 300px;
        overflow: auto;
    }

    .mint-field .mint-cell-title {
        min-width: 105px;
        max-width: 200px;
    }

    .mint-cell-text {
        max-width: 200px;
        display: block;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .mint-cell-value {
        max-width: 200px;
    }

    .my-icon-button {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background-color: #26a2ff;
        color: #fff;
        line-height: 30px;
        text-align: center;
    }

    .cls-span {
        line-height: 50px;
        font-size: 18px;
        text-align: center;
        color: #0e86fe;
        border: 1px solid #999999;
        margin-right: 10px;
        margin-left: 10px;
        display: inline-block;
        border-radius: 20px;
    }

    .sign-cls-div {
        margin: 10px;
        padding: 10px;
        outline: none;
        border-radius: 20px;
        border: solid green;
    }

    .aLine {
        margin: 3px 0 3px 0;
        background-color: #E6E3E3;
        height: 1px;
    }

</style>
<script id="form-tmpl" type="text/html">
    <div class="full" id="form">
        <span class="cls-span" @click="dealNodeClick">{{dealNodeName}}</span>
        <div>
            <div v-for="v in fields" v-if="deal == true">
                <mt-field :label="v.title" v-if="v.leipiplugins == 'text'" :placeholder="v.title"
                          v-model="attrs[v.title]"></mt-field>
                <mt-cell v-if="v.leipiplugins == 'macros'" :title="v.title" :value="attrs[v.title]"
                         v-show="v.orghide == 0">
                </mt-cell>
                <mt-cell v-if="v.leipiplugins == 'select'" :title="v.title" is-link @click.native="select(v)"
                         :value="attrs[v.title]">
                </mt-cell>
                <mt-field v-if="v.leipiplugins == 'textarea'" :label="v.title" :placeholder="v.title" type="textarea"
                          rows="3" v-model="attrs[v.title]"></mt-field>
                <mt-cell v-if="v.leipiplugins == 'datetime'" :title="v.title" is-link @click.native="pickTime(v)"
                         :value="attrs[v.title]">
                </mt-cell>
                <mt-cell v-if="v.leipiplugins == 'sign'" :title="v.title" @click.native="sign(v)">
                    <img width="40" height="40" :src="formatSign(attrs[v.title])">
                </mt-cell>
                <mt-field :label="v.title" :placeholder="v.title" type="textarea" v-if="v.leipiplugins == 'chooseuser'"
                          rows="2" :value="formatChooseUser(attrs[v.title])"
                          @click.native="xuanrenkongjian(v.title, attrs[v.title])">
                    <img width="40" height="40" src="../images/user.png">
                </mt-field>

                <mt-cell v-if="v.leipiplugins == 'chooseimage'" :title="v.title" @click.native="uploadChooseImage(v)">
                    <img width="60" height="60" :src="formatUploadChooseImage(attrs[v.title])">
                </mt-cell>

                <mt-field v-if="v.leipiplugins == 'checkboxs'" :label="v.title" :placeholder="v.title" type="textarea"
                          rows="3" v-model="attrs[v.title]" @click.native="checkboxsClick(v)"></mt-field>

                <mt-field v-if="v.leipiplugins == 'radios'" :label="v.title" :placeholder="v.title" type="textarea"
                          rows="2" v-model="attrs[v.title]" @click.native="radiosClick(v)"></mt-field>

                <div v-if="v.leipiplugins == 'annex'" title="v.title"
                     style="margin-top: 5px;margin-right: 5px;margin-bottom: 5px;">

                    <span style="padding-right: 5px;">{{v.title}}</span>
                    <mt-button height="40" type="default" @click="filesClick(v)" size="small">上传
                    </mt-button>

                    <div>
                        <div v-for="(upload,index) in annexFormat(attrs[v.title])">
                            <mt-cell :title="upload.name" v-if="upload.action == 'upload'">
                                <mt-button height="40" type="default" @click="annexDownload(index,attrs[v.title])"
                                           size="small">下载
                                </mt-button>
                                <mt-button height="40" v-if="upload.creator == uid" type="default"
                                           @click="annexDel(index,v.title)"
                                           size="small" style="margin-left: 15px;">删除
                                </mt-button>
                            </mt-cell>
                        </div>
                    </div>
                </div>

            </div>
            <div v-if="data.sign" class="sign-cls-div">
                <mt-field label="会签意见" placeholder="会签意见" type="textarea"
                          rows="3" v-model="huiqian_text"></mt-field>
                <div class="aLine"></div>
                <mt-button height="40" type="default" @click="signFilesClick()" size="small">上传附件
                </mt-button>
                <div class="aLine"></div>
                <div v-for="(upload,index) in signFiles">
                    <mt-cell :title="upload.name" v-if="upload.action == 'upload'">
                        <mt-button height="40" type="default" @click="signDownload(index)" size="small">下载</mt-button>
                        <mt-button height="40" v-if="upload.creator == uid" type="default" @click="signDelUpload(index)"
                                   size="small" style="margin-left: 15px;">删除
                        </mt-button>
                    </mt-cell>
                </div>

            </div>
        </div>


        <div>
            <div v-for="(upload,index) in files">
                <mt-cell :title="upload.name" v-if="upload.action == 'upload'">
                    <mt-button height="40" type="default" @click="download(index)" size="small">下载</mt-button>
                    <mt-button height="40" v-if="upload.creator == uid" type="default" @click="delUpload(index)"
                               size="small" style="margin-left: 15px;">删除
                    </mt-button>
                </mt-cell>
            </div>
        </div>


        <div style="text-align: center; padding-top: 30px" v-if="(deal || fields.length) || data.sign ">
            <mt-button type="primary" @click="allowUpload()" size="small" v-if="allow_upload"
                       style="margin-right: 15px;">上传
            </mt-button>
            <mt-button type="danger" @click="goBack()" size="small" v-if="allow_back"
                       style="margin-right: 15px;">回退
            </mt-button>
            <mt-button type="primary" @click="submit(false)" style="margin-right: 15px;" size="small">保存</mt-button>
            <mt-button type="primary" @click="submit(true)" size="small">提交</mt-button>
            <!--            <mt-button type="primary" @click="testClick()" size="small">testClick</mt-button>-->

        </div>

        <mt-actionsheet
                :actions="selectOptions"
                v-model="selectVisible">
        </mt-actionsheet>

        <mt-datetime-picker
                v-if="timeType != ''"
                @confirm="pickTimeOk"
                ref="datePicker"
                :type="timeType"
                v-model="timeValue">
        </mt-datetime-picker>

        <mt-popup
                ref="checkoutPop"
                v-model="checkVisible">
            <div style="width:225px;height:20px">
                <h5 style="text-align: center;">请选择</h5>
            </div>
            <mt-checklist
                    :title="checkKey"
                    v-model="checkouboxValue"
                    :options="options">
            </mt-checklist>
            <div style="text-align: center; padding-bottom: 20px;">
                <mt-button type="default" @click="checkoutClick(fales)" size="small">取消</mt-button>
                <mt-button type="primary" @click="checkoutClick(true)" size="small">确定</mt-button>
            </div>
        </mt-popup>
        <mt-popup
                ref="radioPop"
                v-model="radioVisible">
            <div style="width:225px;height:20px">
                <h5 style="text-align: center;">请选择</h5>
            </div>
            <mt-radio
                    :title="radioKey"
                    v-model="radioValue"
                    :options="radioOptions">
            </mt-radio>
            <div style="text-align: center; padding-bottom: 20px;">
                <mt-button type="default" @click="radioButton(fales)" size="small">取消</mt-button>
                <mt-button type="primary" @click="radioButton(true)" size="small">确定</mt-button>
            </div>
        </mt-popup>


        <mt-popup
                v-model="popupVisible"
                position="bottom">
            <!--            <mt-radio-->
            <!--                    v-if="popType == 'select'"-->
            <!--                    title="单选框列表"-->
            <!--                    v-model="value"-->
            <!--                    :options="selectOptions"-->
            <!--            >-->
            <!--            </mt-radio>-->
        </mt-popup>


        <mt-palette-button
                content="详"
                direction="lt" class="pb" :radius="80" ref="target_1"
                mainButtonStyle="color:#fff;background-color:#26a2ff;"
                style="right:20px;position:absolute;bottom: 20px; display: none">
            <div class="my-icon-button indexicon icon-popup" @touchstart="sub_log(1)">单</div>
            <div class="my-icon-button indexicon icon-popup" @touchstart="sub_log(2)">图</div>
            <div class="my-icon-button indexicon icon-popup" @touchstart="sub_log(3)">历</div>
        </mt-palette-button>


        <mt-popup
                v-model="goBackVisible">
            <div style="width:225px;height:20px">
                <h5 style="text-align: center;">请输入回退原因</h5>
            </div>
            <div>
                <mt-field placeholder="请输入回退原因" type="textarea" rows="3" v-model="reason"></mt-field>
            </div>
            <div>
                <select name="public-choice" v-model="log_nodes_name" style="width: 90%;height: 30px;">
                    <option :value="coupon.id" v-for="coupon in log_nodes">{{coupon.nodeName}}</option>
                </select>
            </div>
            <div style="text-align: center; padding-bottom: 20px;">
                <mt-button type="default" @click="goBackFalse" size="small">取消</mt-button>
                <mt-button type="primary" @click="goBackTrue" size="small">确定</mt-button>

            </div>
        </mt-popup>


        <!--        <mt-popup-->
        <!--                v-model="chooseUserVisible"-->
        <!--                position="bottom">-->
        <!--            <mt-picker :slots="slots" ref="picker" @change="onValuesChange"></mt-picker>-->
        <!--            <div style="text-align: center; padding-bottom: 20px;">-->
        <!--                <mt-button style="width: 80%;" type="primary" @click="flow">确定</mt-button>-->
        <!--            </div>-->
        <!--        </mt-popup>-->

        <Sign v-if="canSign" :url="formatSign(attrs[signKey])"
              style="position: absolute; left: 0; top: 0; width: 100%; height: 100%;"></Sign>
    </div>
</script>

<script>
    Date.prototype.Format = function (fmt) { //author: meizz
        var o = {
            "M+": this.getMonth() + 1,                 //月份
            "d+": this.getDate(),                    //日
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //分
            "s+": this.getSeconds(),                 //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    const Form = Vue.component("Form", {
            template: $("#form-tmpl").html(),
            data() {
                return {
                    fields: [],
                    attrs: {},
                    popType: "",
                    popupVisible: false,
                    selectVisible: false,
                    selectOptions: [],
                    html: "",
                    value: "",
                    timeType: "",
                    timeValue: "",
                    timeKey: "",

                    type: "",
                    id: "",
                    deal: false,
                    canSign: false,
                    signKey: "",

                    chooseUserVisible: false,

                    download_upload: false,
                    caozuo_index: "",
                    slots: [],
                    next: [],

                    files: [],
                    allow_upload: false,
                    // upload_list: [],
                    // new_upload_files: [],
                    uid: "",
                    ins: {},
                    allow_back: false,
                    goBackVisible: false,
                    // old_files:[]
                    data: {},
                    huiqian_radio: "",
                    huiqian_text: "",
                    reason: "",
                    js_xuanren: "",
                    log_nodes: [],
                    log_nodes_name: "",
                    options: [],
                    checkouboxValue: [],
                    checkKey: "",
                    checkVisible: false,
                    radioValue: "",
                    radioOptions: [],
                    radioKey: "",
                    radioVisible: false,
                    chooseImageKey: "",
                    annexKey: "",
                    dealNode: "",
                    dealNodeName: "",
                    dealCurrents: [],
                    signFiles: []
                }
            },
            methods: {
                formatChooseUser(v) {
                    if (!v) {
                        return ""
                    }
                    v = v.replace("[json]", "");
                    try {
                        return Object.values(JSON.parse(v)).join(",")
                    } catch (e) {
                        return ""
                    }
                },
                // formatCheckbox(v){
                //     if (!v) {
                //         return ""
                //     }
                //     try {
                //         return Object.values(v)
                //     } catch (e) {
                //         return ""
                //     }
                // },
                getUrl() {
                    return './ins.form.html?type=deal&id=' + this.$route.params.id + "&uid=1"
                },
                popupWindows(index) {
                    this.download_upload = true;
                    this.caozuo_index = index;
                },
                sign(v) {
                    this.signKey = v.title;
                    setTimeout(function () {
                        uploadImg();
                    }, 100);
                },
                uploadChooseImage(v) {
                    this.chooseImageKey = v.title;
                    setTimeout(function () {
                        androidUploadChoo();
                    }, 100);
                },
                formatSign(file) {
                    if (!file) {
                        return "../form/sign.png"
                    }
                    if (file.indexOf("[img]") == -1) {
                        return "/api/bpm/workflow/download?path=" + encodeURI(file);
                    } else {
                        return file.replace("[img]", "");
                    }
                },
                formatUploadChooseImage(file) {
                    if (!file) {
                        return "../images/empty.png"
                    }
                    if (file.indexOf("[img]") == -1) {
                        return "/api/bpm/workflow/download?path=" + encodeURI(file);
                    } else {
                        return file.replace("[img]", "");
                    }
                },
                sub_log(val) {
                    switch (val) {
                        case 1:
                            //dan
                            startFragment("form", this.id, this.type)
                            break;
                        case 2:
                            //to
                            startFragment("canvas", this.id, this.type)
                            break;
                        case 3:
                            //li
                            startFragment("log", this.id, this.type)
                            break;
                    }
                    this.$refs.target_1.collapse();
                },
                //弹框打开
                checkboxsClick(v) {
                    this.checkKey = v.title;
                    // this.checkVisible = true;
                    this.options = v.values
                    this.$nextTick(() => {
                        this.$refs.checkoutPop.open();
                    })
                },
                radiosClick(v) {
                    this.radioKey = v.title;
                    this.radioOptions = v.values
                    console.log("xxxxxxxxxx" + this.radioOptions);
                    this.$nextTick(() => {
                        this.$refs.radioPop.open();
                    })
                },
                checkoutClick(panduan) {
                    if (!panduan) {
                        this.$refs.checkoutPop.close();
                        return;
                    }
                    var ckValue = ""
                    this.checkouboxValue.forEach(item => {
                        ckValue += item + ","
                    })
                    ckValue = ckValue.substring(0, ckValue.length - 1);
                    console.log("checkouboxValue" + ckValue);
                    Vue.set(this.attrs, this.checkKey, ckValue);
                    this.$refs.checkoutPop.close();
                },
                radioButton(panduan) {
                    if (!panduan) {
                        this.$refs.radioPop.close();
                        return;
                    }
                    Vue.set(this.attrs, this.radioKey, this.radioValue);
                    this.$refs.radioPop.close();
                    console.log(this.checkouboxValue + "checkouboxValue");
                },
                getDateType(v) {
                    switch (v.orgformat) {
                        case "日期":
                            return "date";
                        case "时间":
                            return "time";
                        default:
                            return "datetime";
                    }
                },
                pickTime(v) {
                    this.timeKey = v.title;
                    this.timeType = this.getDateType(v);
                    if (this.timeValue === "") {
                        this.timeValue = new Date();
                    }
                    this.$nextTick(() => {
                        this.$refs.datePicker.open();
                    })
                    // this.$refs.datePicker.open();
                },
                pickTimeOk(v) {
                    switch (this.timeType) {
                        case "datetime":
                            v = v.Format("yyyy-MM-dd hh:mm:ss");
                            break

                        case "time":
                            v = v.Format("hh:mm:ss");
                            break;

                        case "date":
                            v = v.Format("yyyy-MM-dd");
                            break;
                    }
                    if (this.timeValue === "") {
                        this.timeValue = new Date();
                    }
                    Vue.set(this.attrs, this.timeKey, v);
                    // this.set(this.attrs, this.timeKey, v);
                    // this.attrs.set(this.timeKey, v);
                },
                select(v) {
                    // this.popType = 'select'
                    // this.popupVisible = true;
                    var t = this;
                    this.selectVisible = true;
                    this.selectOptions = v.values.map(e => {
                        return {
                            name: e,
                            method() {
                                t.attrs[v.title] = e;
                            }
                        }
                    })
                },
                /**
                 * 会签提交
                 */
                saveSign: function (gonext, id) {
                    var t = this;
                    console.log("this.id" + this.id);
                    const huiqiansign = {idea: "同意", info: this.huiqian_text};
                    $.ajax({
                        url: "/api/bpm/workflow/countersign",
                        data: JSON.stringify({
                            _id: this.id,
                            sign: huiqiansign,
                            nodeId: this.dealNode,
                            over: gonext
                        }),
                        type: "post",
                        dataType: "json",
                        headers: {
                            "Content-Type": "application/json"
                        },
                        // complete: function(){
                        //     startFragment("refresh",this.id,"refresh");
                        // },
                        success: function (msg) {
                            if (!msg.success) {
                                showMsg(msg.errMessage);
                                return;
                            }
                            if (!gonext) {
                                showMsg("保存成功")
                            } else if (msg.data == 'continue') {
                                //只有最后一个人才可以选人
                                console.log(JSON.stringify(t.attrs));
                                console.log(t.huiqian_text);
                                startFragment("xuanren", id, JSON.stringify(t.attrs), t.huiqian_text);
                            } else {
                                showMsg("提交成功，请等待其他人会签完毕");
                                //重新加载页面
                                startFragment("refresh", id, "refresh", "refresh");
                            }
                        }
                    });
                }
                ,
                submit(gonext) {
                    console.log("this.signFiles" + this.signFiles);
                    var params = this.$route.params;
                    console.log(params.id);
                    //会签的特殊情况
                    //如果我不是主办人，却可以会签
                    if (!this.data.deal && this.data.sign) {
                        this.saveSign(gonext, params.id);
                        return;
                    }

                    var url;
                    // if (params.type == 'pub') {
                    //     url = "/api/bpm/workflow/createIns";
                    // } else {
                    //     if (gonext) {
                    //         url = "/api/bpm/workflow/subIns";
                    //     } else {
                    url = "/api/bpm/workflow/saveIns";
                    // }
                    // }
                    //pub 发布
                    if (params.type != 'pub' && gonext) {
                        this.getDealers();
                        return
                    }
                    //为了避免更改因为同步绑定修改
                    const new_attrs = JSON.parse(JSON.stringify(this.attrs));
                    // this.fields.forEach(item => {
                    //     if (item["leipiplugins"] === "chooseuser") {
                    //         if (this.js_xuanren.length > 0) {
                    //             new_attrs[item["title"]] = this.js_xuanren;
                    //         }
                    //     }
                    // })
                    androidLog(JSON.stringify(new_attrs));
                    var data =
                        {
                            id: params.id,
                            data: new_attrs,
                            mode: params.type,
                            files: this.files,
                            sign: this.data.sign == true ? {
                                idea: this.huiqian_radio,
                                info: this.huiqian_text,
                                files: this.signFiles
                            } : "",
                            validate: gonext
                            // files: jiaoji_new_upload_files.length > 0 ? jiaoji_new_upload_files : this.new_upload_files
                        }
                    androidLog(JSON.stringify(data));
                    $.ajax({
                        url: url, //"/api/bpm/workflow/createIns",
                        type: "post",
                        headers: {
                            "Content-Type": "application/json"
                        },
                        dataType: "json",
                        data: JSON.stringify(data),
                        success: msg => {
                            if (!msg.success) {
                                return showMsg(msg.errMessage)
                            }

                            if (params.type == 'pub') {
                                if (gonext) {
                                    this.getDealers(msg.data);
                                } else {
                                    showMsg("保存成功");
                                    startFragment("baocun", msg.data, "deal", "refresh");
                                }

                            } else {

                                if (gonext) {

                                    if (msg.data.res == true) {
                                        this.getDealers(msg.data.id);
                                    } else if (msg.data == "can-choose") {
                                        this.getDealers(this.id);
                                    } else {
                                        showMsg("保存成功");
                                        startFragment("baocun", params.id, "deal", "refresh");
                                    }
                                } else {
                                    showMsg("保存成功");
                                    startFragment("baocun", params.id, "deal", "refresh");
                                }

                            }
                        }
                    });
                }
                ,
                load() {
                    this.chooseUserVisible = false;
                    var params = this.$route.params;
                    this.type = params.type;
                    this.id = params.id;
                    var url = "/api/bpm/workflow/getInsInfo";
                    $.post(url, JSON.stringify({
                        id: params.id,
                        args: {type: params.type, logId: this.dealNode},
                        nodeId: this.dealNode
                    }), (msg) => {
                        if (!msg.success) {
                            return
                        }
                        this.data = msg.data;
                        var t = this;
                        this.dealNodeName = this.dealNodeName != "" ? this.dealNodeName : msg.data.dealCurrents[0]["nodeName"]
                        this.dealCurrents = msg.data.dealCurrents
                        this.dealNode = this.dealNode != "" ? this.dealNode : msg.data.dealCurrents[0]["logId"]
                        this.deal = msg.data.deal;
                        this.allow_upload = msg.data.upload
                        this.allow_back = msg.data.back;
                        //判断当前节点是否可以回退，如果可以则赋值

                        if (msg.data.back) {
                            var log = {};
                            (this.data.logs || []).forEach(function (v) {
                                if (v.type == 'save' && v.nodeId != msg.data.current[0]) {
                                    //这一步去重取，最近的数据
                                    log[v.nodeId] = {id: v.id, nodeName: v.nodeName}
                                }
                            });
                            var arr = new Array();
                            for (var key  in log) {
                                arr.push(log[key]);
                            }
                            this.log_nodes = arr;
                            this.log_nodes_name = this.log_nodes[0].id;
                        }


                        this.attrs = msg.data.attrs || {};
                        this.fields = msg.data.formFields
                            .map(e => msg.data.allFields[e])
                            .filter(e => e);


                        //给他附上默认值
                        msg.data.formFields
                            .map(e => {
                                    msg.data.allFields[e]

                                    if (msg.data.allFields[e].leipiplugins === "select") {
                                        console.log("msg.data.allFields[e]msg.data.allFields[e]" + JSON.stringify(msg.data.allFields[e].title));
                                        if (typeof t.attrs[msg.data.allFields[e].title] === 'undefined' || t.attrs[msg.data.allFields[e].title] === "") {
                                            t.attrs[msg.data.allFields[e].title] = msg.data.allFields[e].defaultValue;
                                        }
                                    }
                                }
                            )
                        if (typeof (msg.data.files) != "undefined") {
                            this.files = msg.data.files;
                        }
                    }, "json");
                }
                ,
                allowUpload() {
                    upload("files");
                }
                ,
                dealNodeClick() {
                    var dealCurrents = this.dealCurrents
                    // if(dealCurrents.length < 1){
                    checkNode(JSON.stringify(dealCurrents), this.dealNode)
                    // }
                },
                goBack() {
                    // console.log("")
                    // $.get("/api/bpm/workflow/getLogNodes", {id: this.id}, (msg) => {
                    //     this.log_nodes = msg.data[0];
                    // }, "json");
                    this.goBackVisible = true;
                }
                ,

                goBackTrue() {
                    var back = {
                        logId: this.log_nodes_name,
                        reason: this.reason,
                        sendMobile: ""

                    }
                    $.ajax({
                        url: "/api/bpm/workflow/goBack",
                        data: JSON.stringify({
                            id: this.$route.params.id,
                            back: back
                        }),
                        type: "post",
                        dataType: "json",
                        headers: {
                            "Content-Type": "application/json"
                        },
                        success: function (msg) {
                            if (!msg.success) {
                                showMsg(msg.errMessage);
                                return;
                            }
                            if (!msg.success) {
                                showMsg(msg.errMessage)
                                this.goBackVisible = false;
                                return;
                            }
                            showMsg("回退成功");
                            //重新加载页面
                            this.goBackVisible = false;
                            startFragment("refresh", "", "refresh", "refresh");
                            // onBack();
                        }
                    });

                }
                ,
                goBackFalse() {
                    this.goBackVisible = false;
                }
                ,
                delUpload(index) {
                    this.files.splice(index, 1);
                }, signDelUpload(index) {
                    this.signFiles.splice(index, 1);
                },
                annexDel(index, v) {
                    var annex = this.attrs[v].replace("[annex]", "")
                    let annex_json = JSON.parse(annex)
                    annex_json.splice(index, 1)
                    this.attrs[v] = "[annex]" + JSON.stringify(annex_json)
                }
                ,
                download(index) {
                    androidDownload(JSON.stringify(this.files[index]));
                }, signDownload(index) {
                    androidDownload(JSON.stringify(this.signFiles[index]));
                },
                annexDownload(index, v) {
                    var xx = JSON.stringify(JSON.parse(v.replace("[annex]", ""))[0])
                    androidDownload(JSON.stringify(JSON.parse(v.replace("[annex]", ""))[0]));
                }
                ,
                //附件控件上传按钮
                filesClick(v) {
                    this.annexKey = v.title
                    upload("fileClick");
                },
                signFilesClick() {
                    upload("signFilesClick");
                },
                annexFormat(v) {
                    if (!v) {
                        return ""
                    }
                    v = v.replace("[annex]", "");
                    try {
                        return JSON.parse(v)
                    } catch (e) {
                        return ""
                    }
                },

                getDealers(id) {
                    var attrs = {};
                    try {
                        for (var key in this.attrs) {
                            this.data.formFields.forEach(e => {
                                if (key == e) {
                                    attrs[key] = this.attrs[key]
                                }
                            })

                        }
                    } catch (e) {

                    }
                    startFragment("xuanren", typeof (id) != "undefined" ? id : this.$route.params.id,
                        JSON.stringify(attrs), this.huiqian_text, JSON.stringify(this.files), this.dealNode);
                    return;
                }
                ,
                onValuesChange(picker, e) {
                    for (let i = 0; i < this.next.length; i++) {
                        if (this.next[i].nodeName == e[0]) {
                            picker.setSlotValues(1, (this.next[i].dealers || [])
                                .map(e => e.utname + " " + e.uname)
                            );
                        }
                    }
                }
                ,
                //时间控件
                shijiankong() {
                    const dateBind = this.fields;

                    dateBind.forEach(item => {
                        const expression = item["expression"];
                        if (!expression) {
                            return
                        }
                        Vue.set(this.attrs, item["title"], this.calculate_macro(expression, this.attrs) + "");
                    })
                },
                //时间控制计算
                calculate_macro(str, attrs) {
                    var expression = (str || "").replace(/\[\s*(.+?)\s*\]/g, function (a, b) {
                        return "\"" + (attrs[b] || "") + "\"";
                    });
                    var DATE = function (str) {
                        str = str.replace(/-/g, '/');
                        try {
                            return Math.floor(new Date(str).getTime() / (1000 * 60 * 60 * 24)) + "";
                        } catch (e) {
                            return "";
                        }
                    };
                    expression = expression
                        .replace(/（/g, "(")
                        .replace(/）/g, ")");
                    try {
                        var val = eval(expression);
                        if (isNaN(val)) {
                            return "";
                        }
                        return val;
                    } catch (e) {
                        return "";
                    }
                },
                xuanrenkongjian(title, v) {
                    this.signKey = title;
                    //const choo = this.js_xuanren;
                    v = v || "[json]{}";
                    //v = v.replace("[json]", "");
                    androidxuanren(v);
                }
            }
            ,
            mounted() {
                this.uid = this.$route.params.uid;
                this.load()
                eventBus.$on('sign_ok', (url) => {        //赋值给首页的附近医院数据模型
                    showMsg("上传成功");
                    Vue.set(this.attrs, this.signKey, url) //; "[img]/api/bpm/workflow/download?path=" + (url));
                });
                eventBus.$on('upload_files', (file, fileName) => {
                    let file_to_json = JSON.parse(file)
                    showMsg("上传成功");
                    this.files.push(file_to_json)
                })
                eventBus.$on('xuanren_call', (textStr, textJSON) => {
                    Vue.set(this.attrs, this.signKey, "[json]" + (textJSON));
                    //var testJSON = JSON.parse(textJSON);
                    // this.js_xuanren = "[json]" + JSON.stringify(testJSON);
                })
                eventBus.$on('dialog', (dealNode, dealNodeName) => {
                    Object.assign(this.$data, this.$options.data())
                    this.dealNodeName = dealNodeName
                    this.dealNode = dealNode
                    this.load();
                })
                eventBus.$on('sign_upload', (file) => {
                    showMsg("上传成功");
                    this.signFiles.push(JSON.parse(file))
                })
                eventBus.$on('chooseimage', (fileStr) => {
                    showMsg("上传成功");
                    Vue.set(this.attrs, this.chooseImageKey, fileStr);
                })
                eventBus.$on('annex', (jsonData) => {
                    let annex = this.attrs[this.annexKey]
                    if (typeof annex != "undefined" && annex != "") {
                        var json = annex.replace("[annex]", "");
                        try {
                            showMsg("上传成功");
                            var newJSON = JSON.parse(json)
                            newJSON.push(JSON.parse(jsonData))
                            Vue.set(this.attrs, this.annexKey, "[annex]" + JSON.stringify(newJSON));
                        } catch (e) {
                            showMsg("上传失败")
                        }
                    } else {
                        showMsg("上传成功");
                        Vue.set(this.attrs, this.annexKey, "[annex][" + jsonData + "]");
                    }
                })

                setInterval(this.shijiankong, 500);
            }
        })
    ;
    window.showAlert = function (s) {
        eventBus.$emit("sign_ok", s)
    }
    window.upload_files = function (file, fileName) {
        eventBus.$emit("upload_files", file, fileName)
    }
    window.xuanren_call = function (textStr, textJSON) {
        eventBus.$emit("xuanren_call", textStr, textJSON)
    }
    window.chooseimage = function (fileStr) {
        eventBus.$emit("chooseimage", fileStr)
    }
    window.annex_files = function (jsonData) {
        eventBus.$emit("annex", jsonData)
    }
    window.dialog_result = function (dealNode, dealNodeName) {
        eventBus.$emit("dialog", dealNode, dealNodeName)
    }
    window.sign_file_upload = function (file) {
        eventBus.$emit("sign_upload", file)
    }
</script>